package magicboard.view
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import magicboard.classifier.Classifier;
	import magicboard.model.GeometryObject;
	import magicboard.model.Model;
	import magicboard.model.Polyline;
	
	import mx.core.UIComponent;

	public class Board extends UIComponent
	{
	  // --------------------------------------------------------------------------------------------
		public function Board() 
		{
		  _classifier = new Classifier();
			this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); 
    }
        
	  // --------------------------------------------------------------------------------------------
	  [Bindable] public var classifierStatus:String;
	  public function get lastObject():Polyline { return newShape; }
	  
	  public function get boardModel():Model { return _model; }
	  
	  public function set boardModel(m:Model):void { 
	  	_model = m;
	  	reDraw(); 
	  }
	  
	  public var useClassifier : Boolean = true;
		
	  // --------------------------------------------------------------------------------------------
    public function drawShape(tempShape: GeometryObject):Sprite
    {
    	var temp: Sprite = new Sprite();
    	temp.graphics.lineStyle(thickness, color);

      tempShape.draw(temp.graphics);
    	return temp;
    }
        
	  // --------------------------------------------------------------------------------------------
    public function reDraw():void
    {
    	while(numChildren > 0){
   			removeChildAt(0);
 			}
    
    	for each(var obj:GeometryObject in _model.objects){
    		this.addChild(drawShape(obj));	
    	}
    }
        
	  // --------------------------------------------------------------------------------------------
    private function onMouseDown(e:MouseEvent):void
    {
    	var temp:Point = new Point(mouseX,mouseY);
    	newShape = new Polyline(); // kolejne punkty rysowanego ksztaltu
    	s = new Sprite();  // podglad na ekranie
    	
    	s.graphics.lineStyle(thickness, color);
    	this.addChild(s);
    	
    	s.graphics.moveTo(mouseX,mouseY);
    	newShape.addPoint(temp);
    	
    	
    	this.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
    	this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); 
    } 

	  // --------------------------------------------------------------------------------------------
    private function onMouseUp(e:MouseEvent):void 
    {
    	var temp:Point = new Point(mouseX,mouseY);
    	var obj:GeometryObject;
    	newShape.addPoint(temp);
    	
    	if(useClassifier){
		    obj = _classifier.classify(newShape, _model);
    	}
    	else{
    		obj = newShape;
    	}
    	// Add to the model
		  _model.addObject(obj);
    	        	
    	this.removeChild(s);
    	this.addChild(drawShape(obj));
    	this.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
    	// Send event about classification
    	if(newShape == obj)
    		classifierStatus = "This object was not recognized.";
    	else
    		classifierStatus = "Recognized as: " + obj.toString();
    }
        
	  // --------------------------------------------------------------------------------------------
    private function onMouseMove(e:MouseEvent):void 
    {
    	var temp:Point = new Point(mouseX,mouseY);
    	
    	s.graphics.lineTo(mouseX, mouseY);
    	newShape.addPoint(temp);
    }
        
	  // --------------------------------------------------------------------------------------------
    override protected function updateDisplayList(w:Number, h:Number):void
    {
    	var g:Graphics = this.graphics;
    	
    	g.clear();
        g.lineStyle(1, _fgColor, _drawGrid ? 1:0);
    	g.beginFill(_bgColor, 1);
        g.drawRect(0, 0, unscaledWidth, unscaledHeight);
        g.endFill();
        
        if ( _drawGrid )
        {
        	var i : int = _gridSpacing;
        	while ( i < w )
        	{
        		g.moveTo( i, 0 );
        		g.lineTo( i, h );
        		i += _gridSpacing;
        	}
        	i = _gridSpacing;
        	while ( i < h )
        	{
        		g.moveTo( 0, i );
        		g.lineTo( w, i );
        		i += _gridSpacing;
        	}
        }
    }
        
		// --------------------------------------------------------------------------------------------
		// Private members
		private var _classifier: Classifier;
		private var _model : Model = new Model();  // Model trzyma tablice wszystkich wyswietlanych ksztaltow
		private var s : Sprite; // Aktualnie rysyowany obraz. Znika po zakonczeniu rysowania.
		private var newShape : Polyline;  // zapamietywane kolejne punkty rysowanego obrazu.
    private var _fgColor : int = 0x000000;
		private var _bgColor : int = 0xFFFFFF;
		private var _gridSpacing : int = 5;
		private var _drawGrid : Boolean = false;
		private var color : Number;
		private var thickness : Number = 3;

	}
}
